#ifndef CIap2SQLITE_H_
#define CIap2SQLITE_H_

#include <string>
#include <sqlite3.h>
#include <pthread.h>
#include "Iap2Def.h"
#include "CIpodLib.h"

#define PINYIN_NAME                             "PINYIN"                    // 排序方式

#define MEDIA_LIBRARY_NAME                      "medialibrary.db"           // 多媒体数据库文件名
#define ITEM_TABLE                              "item"                      // 表名 -- 歌曲
#define PLAYLIST_TABLE                          "playlist"                  // 表名 -- 播放列表

#define ITEM_ID                                 "id"
#define ITEM_TITLE                              "title"
#define ITEM_TYPE                               "media_type"
#define ITEM_RATING                             "rating"
#define ITEM_DURATION                           "duration"
#define ITEM_ALBUM_ID                           "album_id"
#define ITEM_ALBUM                              "album"
#define ITEM_ALBUM_TRACK_NUMBER                 "album_track_number"
#define ITEM_ALBUM_TRACK_COUNT                  "album_track_count"
#define ITEM_ALBUM_DISC_NUMBER                  "album_disc_number"
#define ITEM_ALBUM_DISC_COUNT                   "album_disc_count"
#define ITEM_ARTIST_ID                          "artist_id"
#define ITEM_ARTIST                             "artist"
#define ITEM_ALBUM_ARTIST_ID                    "album_artist_id"
#define ITEM_ALBUM_ARTIST                       "album_artist"
#define ITEM_GENRE_ID                           "genre_id"
#define ITEM_GENRE                              "genre"
#define ITEM_COMPOSER_ID                        "composer_id"
#define ITEM_COMPOSER                           "composer"
#define ITEM_IS_PART_OF_COMPILATION             "is_part_of_compilation"
#define ITEM_IS_LIKE_SUPPORTED                  "is_like_supported"
#define ITEM_IS_BAN_SUPPORTED                   "is_ban_supported"
#define ITEM_IS_LIKED                           "is_liked"
#define ITEM_IS_BANNED                          "is_banned"
#define ITEM_IS_RESIDENT_ON_DEVICE              "is_resident_on_device"
#define ITEM_CHAPTER_COUNT                      "chapter_count"

#define PLAYLIST_ID                             "id"
#define PLAYLIST_NAME                           "name"
#define PLAYLIST_PARENT_ID                      "parent_id"
#define PLAYLIST_IS_GENIUS_MIX                  "is_genius_mix"
#define PLAYLIST_IS_FOLDER                      "is_folder"
#define PLAYLIST_FILE_TRANSFER_ID               "file_transfer_id"
#define PLAYLIST_IS_ITUNES_RADIO_STATION        "is_itunes_radio_station"
#define PLAYLIST_ITEMS                          "items"
#define PLAYLIST_STRING_MAX                     128

#ifdef __cplusplus
extern "C"{
#endif
class CIap2Sqlite {
public:
    CIap2Sqlite();
    ~CIap2Sqlite();

    int open(std::string path);
    int close();
    int beginTransaction();
    int commitTransaction();
    int insertMediaItem(IAP2MediaItem item);
    int insertMediaPlaylist(IAP2MediaPlaylist playlist);
    int updateMediaPlaylist(uint64 id, uint64 *items, uint32 num);
    int deleteMediaItem(uint64 id);
    int deleteMediaPlaylist(uint64 id);
    int reset();
	int updateMediaPlaylistName(IAP2MediaPlaylist playlist);

    bool isItemExist(IAP2MediaItem item);
	bool isMediaPlaylistExist(IAP2MediaPlaylist playlist);

    void getPlaylist(IAP2MediaList *mediaList);
    void getArtist(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getAlbum(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getGenre(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getSongs(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getComposer(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getAudioBook(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getPodcast(IAP2MediaList *mediaList, bool hideRemoteItems);
    void getiTunesRadio(IAP2MediaList *mediaList);
    void getArtistByGenre(IAP2MediaList *mediaList, uint64 genreid, bool hideRemoteItems);
    void getAlbumByArtist(IAP2MediaList *mediaList, uint64 artistid, bool hideRemoteItems);
    void getAlbumByGenreArtist(IAP2MediaList *mediaList, uint64 genreid, uint64 artistid, bool hideRemoteItems);
    void getAlbumByComposer(IAP2MediaList *mediaList, uint64 composerid, bool hideRemoteItems);
    void getSongsByPlaylist(IAP2MediaList *mediaList, uint64 playlistid, bool hideRemoteItems);
    void getSongsByAlbum(IAP2MediaList *mediaList, uint64 albumid, bool hideRemoteItems);
    void getSongsByArtistAlbum(IAP2MediaList *mediaList, uint64 artistid, uint64 albumid, bool hideRemoteItems);
    void getSongsByGenreArtistAlbum(IAP2MediaList *mediaList, uint64 genreid, uint64 artistid, uint64 albumid, bool hideRemoteItems);
    void getSongsByComposerAlbum(IAP2MediaList *mediaList, uint64 composerid, uint64 albumid, bool hideRemoteItems);
    void getSongsByAudioBook(IAP2MediaList *mediaList, uint64 audiobookid, bool hideRemoteItems);
    void getSongsByPodcast(IAP2MediaList *mediaList, uint64 podcastid, bool hideRemoteItems);
    void getSongsByID(IAP2MediaList *mediaList, uint64 id, bool hideRemoteItems);
private:
    sqlite3 *m_Handle;
    pthread_mutex_t m_SqliteMutex;

    int getSource(const char *sql, IAP2MediaList *mediaList);
    const char *modifyString(std::string str);
    bool isItemRemote(uint64 id);
    void getItemName(uint64 id, std::string *name);

};
#ifdef __cplusplus
}
#endif
#endif /* CIap2SQLITE_H_ */
